package me.yuxiaoyao.lock.interceptor;

import org.springframework.lang.Nullable;

/**
 * @author kerryzhang on 2020/12/22
 */


public class DistributedLockOperation {
    private final String name;
    private final String key;
    private final boolean block;
    private final long timeout;
    private final String lockExecutor;
    private final String keyGenerator;
    private final String toString;

    public DistributedLockOperation(Builder builder) {
        this.name = builder.name;
        this.key = builder.key;
        this.block = builder.block;
        this.timeout = builder.timeout;
        this.lockExecutor = builder.lockExecutor;
        this.keyGenerator = builder.keyGenerator;
        this.toString = builder.getOperationDescription().toString();
    }

    public String getName() {
        return name;
    }

    public String getKey() {
        return key;
    }

    public boolean isBlock() {
        return block;
    }

    public long getTimeout() {
        return timeout;
    }

    public String getLockExecutor() {
        return lockExecutor;
    }

    public String getKeyGenerator() {
        return keyGenerator;
    }

    public String getToString() {
        return toString;
    }


    @Override
    public boolean equals(@Nullable Object other) {
        return (other instanceof DistributedLockOperation && toString().equals(other.toString()));
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public String toString() {
        return this.toString;
    }

    public static class Builder {
        private String name = "";
        private String key;
        private boolean block;
        private long timeout = -1L;
        private String lockExecutor;
        private String keyGenerator;


        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setKey(String key) {
            this.key = key;
            return this;
        }

        public Builder setBlock(boolean block) {
            this.block = block;
            return this;
        }

        public Builder setTimeout(long timeout) {
            this.timeout = timeout;
            return this;
        }

        public Builder setLockExecutor(String lockExecutor) {
            this.lockExecutor = lockExecutor;
            return this;
        }

        public Builder setKeyGenerator(String keyGenerator) {
            this.keyGenerator = keyGenerator;
            return this;
        }


        protected StringBuilder getOperationDescription() {
            StringBuilder result = new StringBuilder(DistributedLockOperation.class.getSimpleName());
            result.append("[").append(this.name);
            result.append("] key='").append(this.key);
            result.append("' | block='").append(this.block);
            result.append("' | timeout='").append(this.timeout);
            result.append("' | lockExecutor='").append(this.lockExecutor);
            result.append("' | keyGenerator='").append(this.keyGenerator).append("'");
            return result;
        }


        public DistributedLockOperation build() {
            return new DistributedLockOperation(this);
        }

    }


}
